package com.icygen.core.svn;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.net.MalformedURLException;

import org.tigris.subversion.svnclientadapter.ISVNClientAdapter;
import org.tigris.subversion.svnclientadapter.SVNClientAdapterFactory;
import org.tigris.subversion.svnclientadapter.SVNClientException;
import org.tigris.subversion.svnclientadapter.SVNRevision;
import org.tigris.subversion.svnclientadapter.SVNUrl;
import org.tigris.subversion.svnclientadapter.commandline.CmdLineClientAdapterFactory;
import org.tigris.subversion.svnclientadapter.javahl.JhlClientAdapterFactory;
import org.tigris.subversion.svnclientadapter.ISVNDirEntry;

public class Repository {
	
	protected SVNUrl _svnUrl;
	
	protected ISVNClientAdapter _svnClient;
	
	public static final String IcygenSvnUrl = "http://25.80.250.71/svn";
	
	public static final String IcygenModulesUrl = IcygenSvnUrl + "/icygen-modules/trunk";
	public static final String IcygenThemesUrl = IcygenSvnUrl + "/icygen-themes/trunk";
	
	
	public static final HashMap<String, String> Libraries = 
				new HashMap<String , String>() {{
				    put("Icygen", IcygenSvnUrl + "/icygen-core/trunk");
				    put("Zend",   "http://framework.zend.com/svn/framework/standard/trunk/library/Zend");
				    put("ZendX",  "http://framework.zend.com/svn/framework/extras/trunk/library/ZendX");
				}};
				
				
	
	public Repository(String url, String user, String password)
	{
		System.out.println("BEGIN Repository");
		
		
		try {
			this._svnUrl = new SVNUrl(url);
		} catch (MalformedURLException e) {
			System.out.println(e.getMessage());
			return;
		}
			
		
		// register the factories
		this._setupClient();
	
		
        // first create the SVNClient from factory
		// SVNClientAdapterFactory.JAVAHL_CLIENT to use JNI client (recommanded)
		// SVNClientAdapterFactory.COMMANDLINE_CLIENT to use command line client
		// You can also get the best client type interface using getBestSVNClientType
		
		try {
			String bestClientType = SVNClientAdapterFactory.getPreferredSVNClientType();
            System.out.println("Using "+bestClientType+" factory");
			this._svnClient = SVNClientAdapterFactory.createSVNClient(bestClientType);
		} catch (SVNClientException e) {
			System.out.println(e.getMessage());
			return;
		}
		
		

		// set username and password if necessary
		this._svnClient.setUsername(user);
		this._svnClient.setPassword(password);

		//	add a listener if you wish
		NotifyListener listener = new NotifyListener();
		this._svnClient.addNotifyListener(listener);
		
		System.out.println("END Repository");
	}
	
	private void _setupClient()
	{
		
        try {
            JhlClientAdapterFactory.setup();
        } catch (SVNClientException e) {
        	System.out.println("can't register 'JhlClientAdapterFactory'");
        }
        
        try {
            CmdLineClientAdapterFactory.setup();
        } catch (SVNClientException e1) {
        	System.out.println("can't register 'CmdLineClientAdapterFactory'");
        }
	}
	
	public InputStream getContent(String path)
									throws SVNClientException
	{
		try {
			//	use the svn commands
			InputStream is = this._svnClient.getContent(this._svnUrl.appendPath(path), SVNRevision.HEAD);
			return is;
		} catch (SVNClientException e) {
			System.out.println("An exception occured while getting remote file :" + e.getMessage());
			throw e;
		}
	}
	
	public ArrayList<String> getList(String path)
								throws SVNClientException
	{
		ArrayList<String> list = new ArrayList<String>();
		
		try {
			//	use the svn commands
			ISVNDirEntry[] dirEntries = this._svnClient.getList(this._svnUrl.appendPath(path), SVNRevision.HEAD, false);
			
			for (ISVNDirEntry entry : dirEntries) {
				list.add(entry.getPath());
			}
			
			return list;
		} catch (SVNClientException e) {
			System.out.println("An exception occured while getting remote file :" + e.getMessage());
			throw e;
		}
	}
	
	public void doExport(String path, String destPath)
	{
		try {
			this._svnClient.doExport (this._svnUrl.appendPath(path), new File(destPath), SVNRevision.HEAD, true);
		} catch (SVNClientException e) {
			System.out.println("An exception occured while getting remote file :" + e.getMessage());
		}
	}
}
